package com.ruijie.autotest.log.validation.common;

import com.minelx.landisk.filesystem.Paths;
import com.minelx.landisk.filesystem.core.Directory;
import com.minelx.landisk.filesystem.core.File;
import com.ruijie.autotest.log.core.data.ITable;
import com.ruijie.autotest.log.impl.parse.ColumnsAndValuesParser;
import com.ruijie.autotest.log.impl.parse.LinesParser;
import com.ruijie.autotest.log.impl.parse.SplitCellsParser;
import com.ruijie.autotest.log.impl.parse.XmlColumnsParser;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;

import java.io.IOException;
import java.util.List;

import static com.minelx.landisk.filesystem.core.File.byExtension;

public class ExtractZipMainTest {
	private static final Directory tempDirectory = Paths.root("/D:/Desktop/temp");

	public static void main(String[] args) throws IOException, ZipException {
		Directory rootDirectory = Paths.root("/D:/Desktop/网监打流12.17/网监打流12.17/派博网监");

		List<File> zipFiles = rootDirectory.childrenFiles().filter(byExtension("zip")).all();

		for (File zipFile : zipFiles) {
			ITable table = xmlAndBCPToTable(zipFile);

			// processing detail
			System.out.println(table);
		}
	}

	private static ITable xmlAndBCPToTable(File zipFile) throws IOException, ZipException {
		tempDirectory.clear();
		ZipFile zip = new ZipFile(zipFile.getAbsolutePath());
		// extract all files inside to temp directory
		zip.extractAll(tempDirectory.getAbsolutePath());

		File xml = tempDirectory.childrenFiles().filter(byExtension("xml")).first();
		File bcp = tempDirectory.childrenFiles().filter(byExtension("bcp")).first();
		return parseToTable(xml, bcp);
	}

	private static ITable parseToTable(File xml, File bcp) {
		return new ColumnsAndValuesParser(
				new XmlColumnsParser(),
				new LinesParser("\r\n"),
				new SplitCellsParser("\t")).parseTable(xml, bcp);
	}
}
